package guowenlong.golf.ui.regist.step_one;


import android.os.Bundle;
import android.text.Editable;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;

import butterknife.BindView;
import butterknife.OnClick;
import guowenlong.golf.C;
import guowenlong.golf.R;
import guowenlong.golf.base.BaseFragment;
import guowenlong.golf.entity.VerificationCodeEntity;
import guowenlong.golf.ui.regist.RegistActivity;
import guowenlong.golf.widget.IOSEditText;
import me.wenlong.mixutils.MixUtils;

public class StepOneFragment extends BaseFragment<StepOnePresenter, StepOneModel> implements
    StepOneContract.View {

  @BindView(R.id.et_phonenumber)
  IOSEditText etPhonenumber;
  @BindView(R.id.btn_getCode)
  Button btnGetCode;
  @BindView(R.id.cb_accept)
  CheckBox cbAccept;

  @Override
  public int getLayoutId() {
    return R.layout.fragment_step_one;
  }

  @Override
  public void initView() {
    etPhonenumber.setTextChangedAfterListener(new IOSEditText.TextChangedAfterListener() {
      @Override
      public void onTextChangedAfter(Editable s) {
        if (s.toString().length() != 11) {
          btnGetCode.setBackgroundResource(R.drawable.bg_green_regist_normal);
          btnGetCode.setClickable(false);
        } else {
          btnGetCode.setBackgroundResource(R.drawable.bg_green_regist_pressed);
          btnGetCode.setClickable(true);
        }
      }
    });
    etPhonenumber.requestFocus();
  }

  @Override
  public void initData(Bundle savedInstanceState) {

  }

  @OnClick(R.id.btn_getCode)
  public void onClick() {
    if (!cbAccept.isChecked()) {
      Toast.makeText(getActivity(), "您还未同意相关条款及隐私政策", Toast.LENGTH_SHORT).show();
      return;
    }
    mPresenter.getCode(etPhonenumber.getText().toString().trim());
  }

  @Override
  public void getCodeSuccess(VerificationCodeEntity verificationCodeEntity) {
    Toast.makeText(getActivity(), verificationCodeEntity.getMessage(), Toast.LENGTH_SHORT).show();
    ((RegistActivity) getActivity()).selectFragment(C.FRAGMENT_REGIST_STEP2);
    MixUtils.mSp.edit().putString(C.PHONE_NUMBER, etPhonenumber.getText().toString().trim())
        .commit();
  }

  @Override
  public void getCodeError(VerificationCodeEntity verificationCodeEntity) {
    Toast.makeText(getActivity(), verificationCodeEntity.getError_message(), Toast.LENGTH_SHORT)
        .show();
  }
}
